import socket

# 创建 UDP 套接字
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
udp_socket.bind(('127.0.0.1', 9998))

# 目标 IP 地址和端口
dest_ip = '127.0.0.1'
dest_port = 9999


data_frame = bytes([0xff, 0xee, 0x00,0x0a,0x01,0x02,0x03,0x04,0x05,0x6,0x7,0x8,0x9,0xa,0xee,0xff,
0xff,0xee,0x00, 0x05,0x02,0x02,0x03,0x04,0x05,0xee,0xff])
# data_frame = [0xff, 0xee, 0x0a,0x01,0x02,0x03,0x04,0x05,0x6,0x7,0x8,0x9,0xa,0xee,0xff,
# 0xff,0xee,0x05,0x02,0x02,0x03,0x04,0x05,0xee,0xff]

while True:
    # 输入要发送的消息
    message = input("请输入要发送的消息: ")
    # message = b"Hello, UDP!"
    print(type(message))
    print(type(data_frame))
    # 发送消息
    # udp_socket.sendto(message.encode(), (dest_ip, dest_port))

    udp_socket.sendto(data_frame, (dest_ip, dest_port))
    udp_socket.sendto(data_frame[1:], (dest_ip, dest_port))
    udp_socket.sendto(data_frame, (dest_ip, dest_port))
    udp_socket.sendto(data_frame, (dest_ip, dest_port))
    udp_socket.sendto(data_frame, (dest_ip, dest_port))
    udp_socket.sendto(data_frame, (dest_ip, dest_port))
    

    print(f"消息已发送至 {dest_ip}:{dest_port}")

# 关闭套接字
udp_socket.close()